﻿using System.Collections.Specialized;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using SellAvi.Classes;
using SellAvi.Properties;

namespace SellAvi.ViewModel
{
    public class AppSettingsViewModel : ViewModelBase
    {
        public static RelayCommand<Window> SaveUserSettingsCommand { get; set; }
        public static RelayCommand<Window> RestoreUserSettingsCommand { get; set; }
        public static RelayCommand<string> NavigateToCommand { get; set; }
        public static RelayCommand<int> ParserPhoneModeCommand { get; set; }
        public static RelayCommand<int> WebClientAgentCommand { get; set; }
        public string CaptchaService { get; set; }
        public bool CaptchaIsAutomatic
        {
            get
            {
                return Settings.Default.CaptchaIsAutomatic;
            }
            set
            {
                Settings.Default.CaptchaIsAutomatic = value;
            }
        }

        public string CaptchaToken
        {
            get
            {
                return Settings.Default.CaptchaToken;
            }
            set
            {
                Settings.Default.CaptchaToken = (string)value.Trim();
            }
        }

        public int CaptchaTimeout
        {
            get
            {
                return Settings.Default.CaptchaTimeOut;
            }
            set
            {
                Settings.Default.CaptchaTimeOut = value;
            }
        }
        public int CaptchaTrials
        {
            get
            {
                return Settings.Default.CaptchaTrials;
            }
            set
            {
                Settings.Default.CaptchaTrials = value;
            }
        }

        public int PublicationTimeout
        {
            get
            {
                return Settings.Default.PublicationTimeOut;
            }
            set
            {
                Settings.Default.PublicationTimeOut = value;
            }
        }

        public bool PublicationSkipErrors
        {
            get
            {
                return Settings.Default.PublicationSkipErrors;
            }
            set
            {
                Settings.Default.PublicationSkipErrors = value;
            }
        }

        public bool PublicationCommonUserRegion
        {
            get
            {
                return Settings.Default.PublicationCommonUserRegion;
            }
            set
            {
                Settings.Default.PublicationCommonUserRegion = value;
            }
        }
        public int ParserPolling
        {
            get
            {
                return Settings.Default.ParserPolling;
            }
            set
            {
                Settings.Default.ParserPolling = value;
            }
        }
        public bool ParserTrimHtml
        {
            get
            {
                return Settings.Default.ParserTrimHtml;
            }
            set
            {
                Settings.Default.ParserTrimHtml = value;
            }
        }
        public bool ParserDownloadImages
        {
            get
            {
                return Settings.Default.ParserDownloadImages;
            }
            set
            {
                Settings.Default.ParserDownloadImages = value;
            }
        }
        public bool ParserShowErrors
        {
            get
            {
                return Settings.Default.ParserShowErrors;
            }
            set
            {
                Settings.Default.ParserShowErrors = value;
            }
        }
        public bool ParserAllowDuplicates
        {
            get
            {
                return Settings.Default.ParserAllowDuplicates;
            }
            set
            {
                Settings.Default.ParserAllowDuplicates = value;
            }
        }

        public bool ParserDownloadPhones
        {
            get
            {
                return Settings.Default.ParserDownloadPhones;
            }
            set
            {
                if (value)
                {
                    CheckIfVisualCInstalled();
                }
                Settings.Default.ParserDownloadPhones = value;
            }
        }
        public bool ParserTrimPrice
        {
            get
            {
                return Settings.Default.ParserTrimPrice;
            }
            set
            {
                Settings.Default.ParserTrimPrice = value;
            }
        }
       

        public StringCollection ParserPhoneMode
        {
            get
            {
                return Settings.Default.ParserPhoneMode;
            }
            set
            {
                Settings.Default.ParserPhoneMode = value;
            }
        }

        public StringCollection WebClientAgents
        {
            get
            {
                return Settings.Default.WebClientAgents;
            }
            set
            {
                Settings.Default.WebClientAgents = value;
            }
        }

        public int ActivationPagesNum
        {
            get
            {
                return Settings.Default.ActivationPagesNum;
            }
            set
            {
                Settings.Default.ActivationPagesNum = value;
            }
        }

        public int ActivationTimeOut
        {
            get
            {
                return Settings.Default.ActivationTimeOut;
            }
            set
            {
                Settings.Default.ActivationTimeOut = value;
            }
        }
        public string TorExitNodes
        {
            get
            {
                return Settings.Default.TorExitNodes;
            }
            set
            {
                Settings.Default.TorExitNodes = value;
            }
        }
        public bool TorInPriority
        {
            get
            {
                return Settings.Default.TorInPriority;
            }
            set
            {
                Settings.Default.TorInPriority = value;
            }
        }
        public string ProxyServer
        {
            get
            {
                return Settings.Default.ProxyServer;
            }
            set
            {
                Settings.Default.ProxyServer = value;
            }
        }
        public string ProxyLogin
        {
            get
            {
                return Settings.Default.ProxyLogin;
            }
            set
            {
                Settings.Default.ProxyLogin = value;
            }
        }
        public string ProxyPassword
        {
            get
            {
                return Settings.Default.ProxyPassword;
            }
            set
            {
                Settings.Default.ProxyPassword = value;
            }
        }


        private void MoveIntexToTop(int index, StringCollection collection)
        {
            var selectedValue = collection[index];
            collection.RemoveAt(index);
            collection.Insert(0, selectedValue);
        }

        public AppSettingsViewModel()
        {
            SaveUserSettingsCommand = new RelayCommand<Window>((w) =>
            {
                Settings.Default.Save();
                w.Close();
            });
            RestoreUserSettingsCommand = new RelayCommand<Window>((w) => { Settings.Default.Reset(); w.Close(); });
            NavigateToCommand = new RelayCommand<string>((link) => IEHelpers.OpenWebBrowser(link));
            ParserPhoneModeCommand = new RelayCommand<int>((i) => this.MoveIntexToTop(i, ParserPhoneMode));
            WebClientAgentCommand = new RelayCommand<int>((i) => { this.MoveIntexToTop(i, WebClientAgents); ShowWarn();});
        }

        private void ShowWarn()
        {
            MessageBox.Show("Настройки вступят в силу только после перезапуска программы", "Требуется перезапуск",
                MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        /// <summary>
        /// Проверка библиотек Visual C++ Redistributable для работы с Tesseract
        /// </summary>
        public void CheckIfVisualCInstalled()
        {

            RegistryKey x64key = Registry.LocalMachine;
            RegistryKey x32key = Registry.LocalMachine;
            x64key = x64key.OpenSubKey(@"SOFTWARE\Classes\Installer\Dependencies\{323dad84-0974-4d90-a1c1-e006c7fdbb7d}", false);
            x32key = x32key.OpenSubKey(@"SOFTWARE\Classes\Installer\Dependencies\{462f63a8-6347-4894-a1b3-dbfe3a4c981d}", false);
            if (x32key == null && x64key == null)
            {

                var d =
                    MessageBox.Show(
                        "Для корректной работы необходимо загрузить библиотеку для обработки телефонных номеров Microsoft Visual C++ 2015 Redistributable",
                        "Microsoft Visual C++ 2015 Redistributable", MessageBoxButton.YesNo);
                if (d == MessageBoxResult.Yes)
                {
                    IEHelpers.OpenWebBrowser("https://www.microsoft.com/ru-ru/download/details.aspx?id=48145");
                    //TaskProductUploader.OpenWebBrowser(Environment.Is64BitOperatingSystem
                    //    ? "http://download.microsoft.com/download/2/a/2/2a2ef9ab-1b4b-49f0-9131-d33f79544e70/vc_redist.x64.exe"
                    //    : "http://download.microsoft.com/download/9/a/2/9a2a7e36-a8af-46c0-8a78-a5eb111eefe2/vc_redist.x86.exe");
                }
            }



        }
    }
}
